﻿@model Apps.Models.V_LMS_TEMPLATEDETAIL
@using Apps.Web.Core;
@using Apps.Common;
@using Apps.Models.LMS;
@using Apps.Web;
@using Apps.Locale;
@using Apps.Models.Sys;
@{
    ViewBag.Title = "创建";
    Layout = "~/Views/Shared/_Index_LayoutEdit.cshtml";
    List<permModel> perm = null;
}

<script type="text/javascript">
    $(function () {
        function GetPagerFormData() {
            var model = {
                TEMPLATE_NAME: $("#TEMPLATE_NAME").val(),
                TEMPLATE_TYPE: $("#TEMPLATE_TYPE").val(),
                TEMPLATE_DESC: $("#TEMPLATE_DESC").val(),
                INDEX_NAME: JSON.stringify($('#List').datagrid('getChanges')),
                INDEX_ID: JSON.stringify(changeRow)
            };

            return model;
        }

        $("#btnSave").click(function () {
            if (editIndex != undefined) {
                return window.parent.frameReturnByMes("存在未保存的化验复检指标数据，请先保存");
            }
            if ($("form").valid()) {
                $.ajax({
                    url: "@Url.Action("CreateParent")",
                    type: "Post",
                    data: GetPagerFormData(),
                    dataType: "json",
                    success: function (data) {
                        if (data.type == 1) {
                            window.parent.frameReturnByMes(data.message);
                            window.parent.frameReturnByReload(true);
                            window.parent.frameReturnByClose()
                        }
                        else {
                            window.parent.frameReturnByMes(data.message);
                        }
                    }
                });
            }
        return false;
    });
    $("#btnReturn").click(function () {
         window.parent.frameReturnByClose();
    });
});
</script>
<div class="mvctool bgb">
    @Html.ToolButton("btnSave", "fa fa-save", Resource.Save, ref perm, "Save", true)
    @Html.ToolButton("btnReturn", "fa fa-reply", Resource.Reply, false)
</div>
@using (Html.BeginForm())
{
    <table class="formtable">
        <tbody>
            <tr>
                <th style="width:80px">
                    <label for="PERIODNAME"><font color="red">*</font>报单名称</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="PERIODNAME" name="PERIODNAME" type="text" value="" autocomplete="off">
                </td>
                <th style="width:80px">
                    <label for="PERIODNAME"><font color="red">*</font>报单别名</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="PERIODNAME" name="PERIODNAME" type="text" value="" autocomplete="off">
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="PERIODNAME"><font color="red">*</font>参考值周期</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="PERIODNAME" name="PERIODNAME" type="text" value="" autocomplete="off">
                </td>
                <th style="width:80px">
                    <label for="PERIODNAME"><font color="red">*</font>启用状态</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="PERIODNAME" name="PERIODNAME" type="text" value="" autocomplete="off">
                </td>
            </tr>
            <tr>
                <th style="width:80px">
                    <label for="PERIODNAME"><font color="red">*</font>排序</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="PERIODNAME" name="PERIODNAME" type="text" value="" autocomplete="off">
                </td>
                <th style="width:80px">
                    <label for="PERIODNAME"><font color="red">*</font>备注</label>：
                </th>
                <td>
                    <input class="text-box single-line" data-val="true" data-val-required="字段是必需的。" id="PERIODNAME" name="PERIODNAME" type="text" value="" autocomplete="off">
                </td>
            </tr>
            <tr>
                <td colspan="5" style="vertical-align: top; ">
                    <table id="List"></table>
                </td>
            </tr>
        </tbody>
    </table>
}
<script type="text/javascript">
    var DataList = @Html.Raw(ViewBag.TypeList);
    var editIndex = undefined;
    var changeRow = new Array();
    $(function () {
        $('#List').datagrid({
            url: '@Url.Action("GetListDetail")?parentId=0',
            width: SetGridWidthSub(30),
            methord: 'post',
            height: 550,
            fitColumns: true,
            sortName: 'ID',
            sortOrder: 'desc',
            idField: 'ID',
            pageSize: 15,
            pageList: [15, 20, 30, 40, 50],
            pagination: false,
            striped: true, //奇偶行是否区分
            singleSelect: true,//单选模式
            rownumbers: true,//行号
            onLoadSuccess: function(data) {                @foreach(var r in perm){ if (r.Category == 2) { @Html.Raw("$(this).datagrid('hideColumn','" + r.KeyCode + "'); "); } }
            },
            columns: [[
                { field: 'ID', title: '化验项编号', width: 80, hidden: true },
                { field: 'INDEX_NAME', title: '化验项', width: 120, sortable: true },
                { field: 'INDEX_DISNAME', title: '别名', width: 50,  sortable: true },
                {
                    field: 'VALUE_BASE', title: '基值', width: 60, sortable: true,
                    editor: {
                        type: 'textbox'
                    }
                },
                {
                    field: 'VALUE_MIN', title: '最小值', width: 60, sortable: true,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 2
                        }
                    }
                },
                {
                    field: 'VALUE_MAX', title: '最大值', width: 60, sortable: true,
                    editor: {
                        type: 'numberbox',
                        options: {
                            precision: 2
                        }
                    }
                },
            ]],
            toolbar: [{
                text: "保存",
                iconCls: "fa fa-save",
                handler: function () {
                    endEditing();
                }
            }],
            onClickRow: function (index) {
                if (editIndex != index) {
                    if (endEditing()) {
                        $('#List').datagrid('selectRow', index)
                            .datagrid('beginEdit', index);
                        editIndex = index;
                    } else {
                        $('#List').datagrid('selectRow', editIndex);
                    }
                }
            },
            onAfterEdit: function (index, row, changes) {
                var change = false;//判断是否需要提交
                var valueDefault = ["VALUE_MAX", "VALUE_MIN", "VALUE_TYPE"];
                var indexId = row['INDEX_ID'];
                if (JSON.stringify(changes) == "{}") {
                    indexId = '';
                } else {
                    for (var i in changes) {
                        if (valueDefault.indexOf(i) != -1 && (changes[i] != '0.00' && changes[i] != '0'&& changes[i] != '')) {
                            change = true;
                            break;
                        } else {
                            if (changes[i] != undefined && (changes[i] != '0' && changes[i].length > 0)) {
                                change = true;
                                break;
                            }
                        }
                    }
                }
                if (change && changeRow.indexOf(indexId) == -1) {
                    changeRow.push(indexId);
                }
                if (!change && changeRow.indexOf(indexId) != -1) {
                    changeRow.splice(changeRow.indexOf(indexId), 1);
                }
            }
        });


        function fmData(value, row) {
            for (var i = 0; i < DataList.length; i++) {
                if (DataList[i].Id == value) {
                    return DataList[i].Name;
                }
            }
            return row.VALUE_TYPE;
        }
        function fmCheck(value, row, index) {
            if (value == 1) {
                return "是";
            } else {
                return "否";
            }
        }


        function endEditing() {
            if (editIndex == undefined) { return true }
            if ($('#List').datagrid('validateRow', editIndex)) {
                $('#List').datagrid('endEdit', editIndex);
                editIndex = undefined;
                return true;
            } else {
                return false;
            }
        }
    });
</script>
